package com.talent.controller;

import com.talent.common.Result;
import com.talent.pojo.entity.NoticeBanner;
import com.talent.service.NoticeBannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 公告轮播控制器
 */
@RestController
@RequestMapping("/api/notice-banners")
@Slf4j
public class NoticeBannerController {

    @Autowired
    private NoticeBannerService noticeBannerService;

    /**
     * 获取轮播列表（按排序顺序）
     */
    @GetMapping
    public Result<List<NoticeBanner>> getList() {
        log.info("获取轮播列表");
        List<NoticeBanner> list = noticeBannerService.getList();
        return Result.success(list);
    }

    /**
     * 批量添加轮播公告
     */
    @PostMapping("/batch")
    public Result<String> batchAdd(@RequestBody Map<String, List<Long>> params) {
        List<Long> noticeIds = params.get("noticeIds");
        log.info("批量添加轮播公告：{}", noticeIds);
        noticeBannerService.batchAdd(noticeIds);
        return Result.success("添加成功");
    }

    /**
     * 删除轮播公告
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除轮播公告：{}", id);
        noticeBannerService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 更新轮播状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateStatus(@PathVariable Long id, @RequestBody Map<String, Integer> params) {
        Integer status = params.get("status");
        log.info("更新轮播公告状态：id={}, status={}", id, status);
        noticeBannerService.updateStatus(id, status);
        return Result.success("更新成功");
    }

    /**
     * 交换排序（用于上移下移）
     */
    @PutMapping("/swap-sort")
    public Result<String> swapSort(@RequestBody Map<String, Long> params) {
        Long id1 = params.get("id1");
        Long id2 = params.get("id2");
        log.info("交换轮播公告排序：id1={}, id2={}", id1, id2);
        noticeBannerService.swapSort(id1, id2);
        return Result.success("排序成功");
    }
}
